package com.ucar.train.service.zhoujinmu.impl;

import com.ucar.train.service.zhoujinmu.MaintenanceService;
import com.ucar.train.service.zhoujinmu.exception.Exception1;
import com.ucar.train.service.zhoujinmu.pattern.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhoujinmu
 * @title MaintenanceServiceImpl
 * @projectName train-parent
 * @description TODO
 * @created 2019-08-22 14:09
 **/
@Service
public class MaintenanceServiceImpl implements MaintenanceService {

    @Override
    public String maintenanceCar() {
        Car car1 = new Car();
        car1.setRepairState(1);

        List<Handler> handlers = new ArrayList<>();
        Handler tiresRepair = new TiresRepair(1);
        Handler engineRepair = new EngineRepair(2);
        Handler filterRepair = new FilterRepair(3);
        Handler bodyRepair = new BodyRepair(4);
        handlers.add(tiresRepair);
        handlers.add(engineRepair);
        handlers.add(filterRepair);
        handlers.add(bodyRepair);
        handlers.get(0).setNext(handlers.get(1));
        handlers.get(1).setNext(handlers.get(2));
        handlers.get(2).setNext(handlers.get(3));
        while (car1.getRepairState() < 5) {
            try {
                tiresRepair.HandleMessage(car1);
            } catch (Exception1 e) {
                System.out.println("保养流程中超时退出，该车退出保养");
                e.printStackTrace();
                break;
            } catch (Exception e) {
                e.printStackTrace();
                break;
            }
        }
        if (car1.getRepairState() >= 5) {
            System.out.println("保养完成");
            return "保养完成";
        }
        System.out.println("保养未完成");
        return "保养未完成";
    }
}
